package com.crm.controller;

import com.crm.entity.CustomerLoss;
import com.crm.entity.CustomerLossReprieve;
import com.crm.service.CustomerLossReprieveService;
import com.crm.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户流失暂缓措施Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/customerLossReprieve")
public class CustomerLossReprieveController {
    @Resource
    private CustomerLossReprieveService customerLossReprieveService;


    /**
     * 分页条件查询客户流失暂缓措施
     *
     * @param customerLossId
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "lossId", required = false) String customerLossId, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerLossId", customerLossId);
        List<CustomerLossReprieve> customerReprieveList = customerLossReprieveService.find(map);
        JSONObject result = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"customerLoss"});
        JSONArray jsonArray = JSONArray.fromObject(customerReprieveList, jsonConfig);
        result.put("rows", jsonArray);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 添加或者修改客户流失暂缓措施
     *
     * @param customerLossReprieve
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(CustomerLossReprieve customerLossReprieve, HttpServletResponse response) throws Exception {
        if (customerLossReprieve.getId() == null) {
            customerLossReprieveService.add(customerLossReprieve);
        } else {
            customerLossReprieveService.update(customerLossReprieve);
        }
        JSONObject result = new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除客户流失暂缓措施
     *
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
        customerLossReprieveService.delete(Integer.parseInt(id));
        JSONObject result = new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}
